#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
#include <QVariant>
#include <QLocale>
#include <QTranslator>
#include <QtGui/QIcon>
#include <QtQuick/QQuickWindow>
#include <QDir>

#include "../deskcore/persistence/settings.h"
#include "../deskcore/ToxDeskApp.h"
#include "ToxMainWnd.h"
#include "videosurface.h"
int main(int argc, char *argv[])
{

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QQuickWindow::setSceneGraphBackend("software");
    QGuiApplication app(argc, argv);
    app.setWindowIcon(QIcon(":/images/ToxDesk.png"));
    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "toxdesk_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            app.installTranslator(&translator);
            break;
        }
    }
    Settings& settings = Settings::getInstance();
    settings.loadGlobal();
    ToxDeskApp& toxapp = ToxDeskApp::getInstance();
    toxapp.setSettings(&settings);
    QString bootstrapNodesFile = QDir::currentPath() + "/bootstrapNodes.json";
    toxapp.loadbootstrapNodes(bootstrapNodesFile.toStdString());
    QString toxDataPath = QDir::currentPath().append("/ToxDesk.tox");
    toxapp.loadProfile(toxDataPath.toStdString(),"toxdesk");
    toxapp.start();
    ToxMainWnd mainWnd;
    mainWnd.init();
    qmlRegisterType<VideoSurface>("toxdesk.VideoSurface",1,0,"VideoSurface");
    QQmlApplicationEngine engine;

    QQmlContext* rootContex = engine.rootContext();//拿到engine的根上下文
    rootContex->setContextProperty("mainWndModel",&mainWnd);//注册

    const QUrl url(QStringLiteral("qrc:/main_pc.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
